package com.codingke.codingkeplayer;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.codingke.codingkeplayer.utils.MediaUtils;
import com.codingke.codingkeplayer.vo.Mp3Info;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

import java.util.ArrayList;

public class PlayActivity extends BaseActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener{

    private TextView textView1_title, textView1_start_time, textView1_end_time;
    private ImageView imageView1_album, imageView1_next, imageView2_play_pause, imageView3_previous, imageView1_play_mode, imageView1_xin;
    private SeekBar seekBar1;
    private ViewPager viewPager;

    private ArrayList<View> views = new ArrayList<>();

//    private ArrayList<Mp3Info> mp3Infos;
    private static final int UPDATE_TIME = 0x1;

    private CodingkePlayerApp app;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activify_music_play);

        app = (CodingkePlayerApp) getApplication();

        textView1_title = (TextView) findViewById(R.id.textView1_title);
        textView1_start_time = (TextView) findViewById(R.id.textView1_start_time);
        textView1_end_time = (TextView) findViewById(R.id.textView1_end_time);
//        imageView1_album = (ImageView) findViewById(R.id.imageView1_album);
        imageView1_next = (ImageView) findViewById(R.id.imageView1_next);
        imageView2_play_pause = (ImageView) findViewById(R.id.imageView2_play_pause);
        imageView3_previous = (ImageView) findViewById(R.id.imageView3_previous);
        imageView1_play_mode = (ImageView) findViewById(R.id.imageView1_play_mode);
        imageView1_xin = (ImageView) findViewById(R.id.imageView1_xin);

        seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        initViewPager();

        imageView2_play_pause.setOnClickListener(this);
        imageView1_next.setOnClickListener(this);
        imageView3_previous.setOnClickListener(this);
        imageView1_play_mode.setOnClickListener(this);
        seekBar1.setOnSeekBarChangeListener(this);
        imageView1_xin.setOnClickListener(this);

        //mp3Infos = MediaUtils.getMp3Infos(this);
        bindPlayService();
        myHandler = new MyHandler(this);
    }

    private void initViewPager(){
        View album_image_layout = getLayoutInflater().inflate(R.layout.album_image_layout, null);
        View irc_layout = getLayoutInflater().inflate(R.layout.irc_layout, null);
        textView1_title = (TextView) album_image_layout.findViewById(R.id.textView1_title);
        imageView1_album = (ImageView) album_image_layout.findViewById(R.id.imageView1_album);
        views.add(album_image_layout);
        views.add(irc_layout);
        viewPager.setAdapter(new MyPagerAdapter());
    }

    public long getId(Mp3Info mp3Info) {
        //初始化收藏状态
        long id = 0;
        switch (playService.getChangePlayList()){
            case PlayService.MY_MUSIC_LIST:
                id = mp3Info.getId();
                break;
            case PlayService.LIKE_MUSIC_LIST:
                id = mp3Info.getMp3InfoId();
                break;
        }

        return id;
    }

    class MyPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View v = views.get(position);
            container.addView(v);
            return v;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }



        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindPlayService();
    }

    private static MyHandler myHandler;

    /**
     * 播放进度条改变时的状态
     * @param seekBar
     * @param progress
     * @param fromUser
     */
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        //如果是来自用户的拖动
        if (fromUser){
            playService.pause();
            playService.seekTo(progress);
            playService.start();
        }
    }

    /**
     * 播放进度条开始拖动的状态
     * @param seekBar
     */
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    /**
     * 播放进度条拖动结束时的状态
     * @param seekBar
     */
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    static class MyHandler extends android.os.Handler{


        private PlayActivity playActivity;
        public MyHandler(PlayActivity playActivity){
            this.playActivity = playActivity;
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (playActivity != null){
                switch (msg.what){
                    case UPDATE_TIME:
                        playActivity.textView1_start_time.setText(MediaUtils.formatTime(msg.arg1));
                        break;
                }
            }
        }
    }

    @Override
    public void publish(int progress) {
        //textView1_end_time.setText(MediaUtils.formatTime(progress));
        Message msg = myHandler.obtainMessage(UPDATE_TIME);
        msg.arg1 = progress;
        myHandler.sendMessage(msg);
        seekBar1.setProgress(progress);
    }

    @Override
    public void change(int position) {
        Mp3Info mp3Info = playService.mp3Infos.get(position);
        textView1_title.setText(mp3Info.getTitle());
        Bitmap albumBitmap = MediaUtils.getArtwork(this, mp3Info.getId(), mp3Info.getAlbumId(), true, false);
        imageView1_album.setImageBitmap(albumBitmap);
        textView1_end_time.setText(MediaUtils.formatTime(mp3Info.getDuration()));
        imageView2_play_pause.setImageResource(R.mipmap.pause);
        seekBar1.setProgress(0);
        seekBar1.setMax((int)mp3Info.getDuration());

        if (playService.isPlaying()){
            imageView2_play_pause.setImageResource(R.mipmap.pause);
        }else{
            imageView2_play_pause.setImageResource(R.mipmap.play);
        }

        switch (playService.getPlay_mode()){
            case PlayService.ORDER_PLAY:
                imageView1_play_mode.setImageResource(R.mipmap.order);
                imageView1_play_mode.setTag(PlayService.ORDER_PLAY);
                break;
            case PlayService.SINGLE_PLAY:
                imageView1_play_mode.setImageResource(R.mipmap.single);
                imageView1_play_mode.setTag(PlayService.SINGLE_PLAY);
                break;
            case PlayService.RANDOM_PLAY:
                imageView1_play_mode.setImageResource(R.mipmap.random);
                imageView1_play_mode.setTag(PlayService.RANDOM_PLAY);
                break;
        }

        //初始化歌曲收藏的状态
        try {
            Mp3Info likeMp3info = app.dbUtils.findFirst(Selector.from(Mp3Info.class).where("mp3InfoId","=", mp3Info.getMp3InfoId()));
            if (likeMp3info != null){
                imageView1_xin.setImageResource(R.mipmap.xin_hong);
            }else{
                imageView1_xin.setImageResource(R.mipmap.xin_bai);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.imageView2_play_pause:
                if (playService.isPlaying()){
                    imageView2_play_pause.setImageResource(R.mipmap.play);
                    playService.pause();
                }else{
                    if(playService.isPause()){
                        imageView2_play_pause.setImageResource(R.mipmap.pause);
                        playService.start();
                    }else{
                        playService.play(playService.getCurrentPosition());
                    }
                }
                break;
            case R.id.imageView1_next:
                playService.next();
                break;
            case R.id.imageView3_previous:
                playService.prev();
                break;
            //切换播放模式
            case R.id.imageView1_play_mode:
                int mode = (int) imageView1_play_mode.getTag();
                switch (mode){
                    case PlayService.ORDER_PLAY:
                        imageView1_play_mode.setImageResource(R.mipmap.random);
                        imageView1_play_mode.setTag(PlayService.RANDOM_PLAY);
                        playService.setPlay_mode(PlayService.RANDOM_PLAY);
                        Toast.makeText(PlayActivity.this, getString(R.string.random_play), Toast.LENGTH_SHORT).show();
                        break;
                    case PlayService.RANDOM_PLAY:
                        imageView1_play_mode.setImageResource(R.mipmap.single);
                        imageView1_play_mode.setTag(PlayService.SINGLE_PLAY);
                        playService.setPlay_mode(PlayService.SINGLE_PLAY);
                        Toast.makeText(PlayActivity.this, getString(R.string.single_play), Toast.LENGTH_SHORT).show();
                        break;
                    case PlayService.SINGLE_PLAY:
                        imageView1_play_mode.setImageResource(R.mipmap.order);
                        imageView1_play_mode.setTag(PlayService.ORDER_PLAY);
                        playService.setPlay_mode(PlayService.ORDER_PLAY);
                        Toast.makeText(PlayActivity.this, getString(R.string.order_play), Toast.LENGTH_SHORT).show();
                }
                break;
            //收藏操作
            case R.id.imageView1_xin:
                Mp3Info mp3Info = playService.mp3Infos.get(playService.getCurrentPosition());
                try {
                    Mp3Info likeMp3info = app.dbUtils.findFirst(Selector.from(Mp3Info.class).where("mp3InfoId","=", getId(mp3Info)));
                    if (likeMp3info == null){
                        mp3Info.setMp3InfoId(mp3Info.getId());
                        mp3Info.setIsLike(1);
                        app.dbUtils.save(mp3Info);
                        imageView1_xin.setImageResource(R.mipmap.xin_hong);
                    }else{
                        int isLike = likeMp3info.getIsLike();
                        if (isLike == 1){
                            likeMp3info.setIsLike(0);
                            imageView1_xin.setImageResource(R.mipmap.xin_bai);
                        }else{
                            likeMp3info.setIsLike(1);
                            imageView1_xin.setImageResource(R.mipmap.xin_hong);
                        }
                        app.dbUtils.update(likeMp3info, "isLike");
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }
}
